package com.woniu108.carService.commons.log;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


@Component
@Aspect
@Slf4j
public class LogAdvice {

    @Around("@annotation(com.woniu108.carService.commons.log.LogInfo)")
    public Object log(ProceedingJoinPoint  pjp) throws Throwable {
        //获取目标方法对象
        MethodSignature ms= (MethodSignature) pjp.getSignature();
        LogInfo info = ms.getMethod().getDeclaredAnnotation(LogInfo.class);
        log.info("============开始执行{}===========",info.value());
        if(info.param()) log.info("参数"+JSONUtil.parseArray(pjp.getArgs()).toString());
        Long startTime=null;
        if(info.time()) startTime = System.currentTimeMillis();
        Object value=pjp.proceed();
        if(info.result()) log.info("结果", JSONUtil.parse(value).toString());
        if(startTime==null){
            log.info("============{}执行结束===========",info.value());
        }
        log.info("============{}执行结束,耗时:{}===========",info.value(),System.currentTimeMillis()-startTime+"毫秒");
            return value;
    }


}
